<template>
  <el-dialog :visible.sync="isDialog"
    width="600px"
    :title="`${item.id?'编辑':'新增'}人员`"
    @close="close">
    <el-form :model="item"
      :rules="rules"
      ref="item"
      label-position="left"
      label-width="100px">
      <el-form-item label="姓名"
        prop="name">
        <el-input :class="$style.formInput"
          v-model="item.name"></el-input>
      </el-form-item>
      <el-form-item label="手机号码"
        prop="phone">
        <el-input v-model="item.contact"
          :class="$style.formInput"></el-input>
      </el-form-item>
    </el-form>

    <span slot="footer">
      <el-button @click="isDialog = false">取 消</el-button>
      <el-button @click="submit"
        type="primary">提 交</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props:['item'],
  data() {
    return {
      isDialog: true,
      type:'',
      form: {},
      rules: {
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        contact: [{ required: true, message: '请输入手机号码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    close(){
      this.$emit("close");
    },
    submit() {
      this.$refs.item.validate(valid => {
        if (!valid) return false
        if(this.item.id){
          return this.edit();
        }else{
          return this.create();
        }
      })
    },
    create(){
      this.$http.post('/Manage/Worker/add',{
        name:this.item.name,
        contact:this.item.contact
      })
      .then(res=>{
        this. isDialog = false;
        this.$emit("getData")
      })
    },
    edit(){
      this.$http.post('/Manage/Worker/edit',{
        id:this.item.id,
        name:this.item.name,
        contact:this.item.contact
      })
      .then(res=>{
        this. isDialog = false;
        this.$emit("getData")
      })
    }
  }
}
</script>

<style module lang="scss">
.formInput {
  width: 250px;
}
</style>
